package wec_wallet.ebo.com.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import bassproject.ebo.com.ebobass.cropimage.ImagePicker;
import bassproject.ebo.com.ebobass.http_utils.interfaces.IHttpPostListener;
import bassproject.ebo.com.ebobass.http_utils.utils.Ipost;
import bassproject.ebo.com.ebobass.http_utils.utils.LoadingUtils;
import bassproject.ebo.com.ebobass.utils.IToast;
import bassproject.ebo.com.ebobass.utils.I_Share;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Response;
import wec_wallet.ebo.com.R;
import wec_wallet.ebo.com.common.Constant;
import wec_wallet.ebo.com.common.HttpUtils;
import wec_wallet.ebo.com.utils.Functions;
import wec_wallet.ebo.com.utils.URLs;

import static wec_wallet.ebo.com.activity.main.MineFragment.CODE_REFRESH;

/**
 * Created by admin on 2018/3/28.
 */

public class IdentityVerificationActivity extends AbsImageActivity {

    @BindView(R.id.iv_identity_down)
    ImageView ivIdentityDown;
    @BindView(R.id.iv_identity_up)
    ImageView ivIdentityUp;
    @BindView(R.id.iv_identity_human)
    ImageView ivIdentityHuman;
    @BindView(R.id.tv_assist1)
    TextView tvAssist1;
    @BindView(R.id.tv_assist2)
    TextView tvAssist2;
    @BindView(R.id.tv_assist3)
    TextView tvAssist3;
    private Map<View, String> mPathMap = new LinkedHashMap<>();
    private ImageView mCurrentClickedView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indentity);
        ButterKnife.bind(this);
    }



    @OnClick(R.id.iv_identity_down)
    public void onIvIdentityDownClicked() {
        mCurrentClickedView = ivIdentityDown;
        mImagePicker.pickImageByRatio(this, ivIdentityDown, ImagePicker.MODE_DEFAULT, 16, 9);
    }

    @OnClick(R.id.iv_identity_up)
    public void onIvIdentityUpClicked() {
        mCurrentClickedView = ivIdentityUp;
        mImagePicker.pickImageByRatio(this, ivIdentityUp, ImagePicker.MODE_DEFAULT, 16, 9);
    }

    @OnClick(R.id.iv_identity_human)
    public void onIvIdentityHumanClicked() {
        mCurrentClickedView = ivIdentityHuman;
        mImagePicker.pickImage(this, ivIdentityHuman);
    }


    @Override
    public void onFetchPhotoPostExcute(Bitmap bitmap, String orignalPath, String cropPath) {
        super.onFetchPhotoPostExcute(bitmap, orignalPath, cropPath);
        mPathMap.put(mCurrentClickedView, cropPath);
        mCurrentClickedView.setImageBitmap(bitmap);
        if (mCurrentClickedView == ivIdentityUp) tvAssist1.setVisibility(View.GONE);
        if (mCurrentClickedView == ivIdentityDown) tvAssist2.setVisibility(View.GONE);
        if (mCurrentClickedView == ivIdentityHuman) tvAssist3.setVisibility(View.GONE);
    }


    //返回
    @OnClick(R.id.actionbar_back)
    void onClick_Back() {
        finish();
    }

    @OnClick(R.id.btn_confirm)
    public void onViewClicked() {
        if (mPathMap.size() != 3) {
            IToast.show(getString(R.string.plz_select_image_to_be_uploaded));
            return;
        }
        LoadingUtils.progressDialogShow(this);
        new Thread(new Runnable() {
            @Override
            public void run() {
                String[][] data = new String[3][2];
                Map<String, String> maps = new HashMap<>();
                maps.put("uid", I_Share.readAccountUserid());
                maps.put("token", I_Share.readAccountToken());
                int i = 0;
                for (Map.Entry<View, String> entry : mPathMap.entrySet()) {

                    try {
                        Response response = HttpUtils.uploadFileSync(Constant.URL_UPLOAD, "thumb", entry.getValue(), maps);
                        if (response.isSuccessful()) {
                            String result = response.body().string();
                            data[i] = parseThumb(result);
                        } else {
                            Message msg = Message.obtain(mHandler);
                            msg.what = -1;
                            msg.obj = new Object();
                            mHandler.sendMessage(msg);
                            return;
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    i++;
                }

                Message msg = Message.obtain(mHandler);
                msg.what = 1;
                msg.obj = data;
                mHandler.sendMessage(msg);

            }
        }).start();
    }


    @Override
    protected void handleMsg(Message msg) {
        super.handleMsg(msg);

        if (msg.what == -1) {
            LoadingUtils.progressDialogDismiss();
            return;
        }
        String[][] data = (String[][]) msg.obj;
        confirmIDCode(data);
    }


    protected void confirmIDCode(String[][] data) {
        Map<String, Object> postParams = new HashMap<>();
        postParams.put("uid", I_Share.readAccountUserid());
        postParams.put("token", I_Share.readAccountToken());
        postParams.put("image_path1", data[0][0]);
        postParams.put("image1", data[0][1]);
        postParams.put("image_path2", data[1][0]);
        postParams.put("image2", data[1][1]);
        postParams.put("image_path3", data[2][0]);
        postParams.put("image3", data[2][1]);

        Functions.post(this, URLs.CHANGE_IDCODE, postParams, new IHttpPostListener() {
            @Override
            public void onTimeOut() {

            }
            @Override
            public void responseSuccess(String result) {
                try {
                    if (Ipost.isSuccess(result)) {
                        IToast.show(getString(R.string.hint_verify_identy_success));
                        setResult(CODE_REFRESH);
                        finish();
                    } else {
                        IToast.show(Ipost.getErrorMsg(result));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                LoadingUtils.progressDialogDismiss();
            }
        }, false, false);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
